package com.ripplezjw.blog.controller.admin;


import com.ripplezjw.blog.Service.ArticleService;
import com.ripplezjw.blog.Service.ClassificationService;
import com.ripplezjw.blog.po.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;

@Controller
@RequestMapping("/admin")
//@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class ArticleInputController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ClassificationService classificationService;

    @GetMapping("/articleInput")
    public String articleInput() {
        return "/admin/input/articleInput";
    }

    @GetMapping("/articleMod/{articleId}")
    public String articleMod(Model model,
                             @PathVariable("articleId") int articleId) {
        Article article = articleService.clickModArticleBtn(articleId);
        model.addAttribute("article", article);
        model.addAttribute("clsName", classificationService.getClsNameByArticleId(articleId));
        return "/admin/input/articleMod";
    }

    @PostMapping("/articleMod/{articleId}/to")
    public String articleModTo(Article article,
                               @PathVariable("articleId") int articleId,
                               @RequestParam("articleTags") String articleTags,
                               @RequestParam("clsName") String classification,
                               RedirectAttributes redirectAttributes) {
        article.setArticleId(articleId);

        Date now = new Date();
        article.setModTime(now);

        String[] tags = articleTags.split("\\+");

        System.out.println(article);
        System.out.println(articleTags);

        String msg = articleService.modArticle(article, tags, classification);
        System.out.println("msg = " + msg);
        // 更新成功
        if (msg.isEmpty()) return "redirect:/admin/articleMan";
        else {
            // 更新失败
            redirectAttributes.addFlashAttribute("article_input_error_msg", msg);
            return "redirect:/admin/articleMod/" + article.getArticleId();
        }
    }

    @GetMapping("/articleDel/{articleId}")
    public String articleDel(Model model,
                             @PathVariable("articleId") int articleId) {
        articleService.clickDelArticleBtn(articleId);
        return "redirect:/admin/articleMan";
    }

    @PostMapping("/articleInputTo")
    public String articleInputTo(Article article,
                                 @RequestParam("articleTags") String articleTags,
                                 @RequestParam("clsName") String classification,
                                 RedirectAttributes redirectAttributes) {
        Date now = new Date();
//        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        String strTime = format.format(now);
        article.setCreateTime(now);
        article.setModTime(now);

        String[] tags = articleTags.split("\\+");

        System.out.println(article);
        System.out.println(articleTags);

        String msg = articleService.addArticle(article, tags, classification);
        // 插入成功
        if (msg.isEmpty()) return "redirect:/admin/articleMan";
        else {
            // 插入失败
            redirectAttributes.addFlashAttribute("article_input_error_msg", msg);
            return "redirect:/admin/articleInput";
        }
    }
}
